package app.beit.fintrack.controller.summarytrack;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import app.beit.fintrack.bean.HospitalBean;
import app.beit.fintrack.bean.NavigationBean;
import app.beit.fintrack.bean.ProvinceBean;
import app.beit.fintrack.bean.ZoneBean;
import app.beit.fintrack.bean.summary.SummaryTrackChartBean;
import app.beit.fintrack.controller.BaseController;
import app.beit.fintrack.controller.ResponseFintrack;
import app.beit.fintrack.controller.bean.SummaryTrackSessionBean;
import app.beit.fintrack.controller.bean.pages.SummaryTrackPageRepNo;
import app.beit.fintrack.service.MasterService;
import app.beit.fintrack.service.summarytrack.SummaryTrackService;
import app.beit.fintrack.utils.DateFormatUtils;
import app.beit.fintrack.utils.constr.MsgConstr;
import app.beit.fintrack.utils.constr.SessionConstr;
import app.beit.fintrack.utils.enums.ESummaryStatus;

@Controller
@RequestMapping("/summary/details")
public class SummaryTrackDetailsController extends BaseController {
	
	private final Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private SummaryTrackService summaryTrackService;
	@Autowired
	private MasterService masterService;
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.GET)
	public String excute(final HttpServletRequest request, final @RequestParam("zoneId") String zoneId, final @RequestParam(value="c", required = false) String c){
		try {			
			this.log.info("forward page to summary details");
			final SummaryTrackSessionBean stsb = (SummaryTrackSessionBean) super.getAttributeInSession(request, SessionConstr.SESSION_SUMMARY);
			final ZoneBean zone = this.masterService.getZoneByZoneId(zoneId);
			SummaryTrackPageRepNo pageRepNo = stsb.getPageRepNo();
			
			if(c != null && c.length() > 0){
				pageRepNo = new SummaryTrackPageRepNo();
			}else{
				if(pageRepNo != null){
					final ZoneBean currentZone = pageRepNo.getZone();
					final String newZoneId = zone.getZoneId();
					final String currentZoneId = currentZone.getZoneId();
					if(!newZoneId.equalsIgnoreCase(currentZoneId)){
						pageRepNo = new SummaryTrackPageRepNo();
					}
				}else{
					pageRepNo = new SummaryTrackPageRepNo();
				}
			}
			pageRepNo.setZone(zone);
			stsb.setPageRepNo(pageRepNo);
			stsb.setUrl("/summary/details?zoneId="+zone.getZoneId());
			stsb.setCurrentPage(2);
			
			final List<NavigationBean> navis = (List<NavigationBean>) request.getSession().getAttribute(SessionConstr.NAVIGATION);
			
			final int thisSort = 2;
			boolean isAddNavi = true;
			final int naviSize = navis.size();
			if(naviSize > thisSort){
				navis.remove(thisSort);
			}
			for(final NavigationBean navi : navis){
				final int sort = navi.getSort();
				if(sort == thisSort){
					isAddNavi = false;
				}			
			}
			
			if(isAddNavi){
				final NavigationBean bean = new NavigationBean();
				bean.setSort(thisSort);
				bean.setName(zone.getZoneName());
				bean.setUrl("");
				navis.add(bean);
			}
			
			stsb.setNavigations(navis);
			
			request.getSession().setAttribute(SessionConstr.SESSION_SUMMARY, stsb);
			request.getSession().setAttribute(SessionConstr.NAVIGATION, navis);
		} catch (Exception e) {
			this.log.error(e.getMessage());
			e.printStackTrace();
		}
		
		return "summaryTrackStation/summaryTrackStationDetails";
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="getRepNo", method=RequestMethod.POST)
	@ResponseBody
	public ResponseFintrack<Map<String, List<SummaryTrackChartBean>>> getRepNo(final HttpServletRequest request, final @RequestParam("fromDate") String fromDate, 
			final @RequestParam("toDate") String toDate, final @RequestParam("hospitalCode") String hospitalCode, final @RequestParam("provinceId") String provinceId,
			final @RequestParam("repNo") String repNo, final @RequestParam("authType") String authType){
		this.log.info("get repno");
		ResponseFintrack<Map<String, List<SummaryTrackChartBean>>> results = null;
		try{
			final SummaryTrackSessionBean stsb = (SummaryTrackSessionBean) super.getAttributeInSession(request, SessionConstr.SESSION_SUMMARY);
			final Map<String, List<SummaryTrackChartBean>> maps = new HashMap<String, List<SummaryTrackChartBean>>();
			final Date fd = DateFormatUtils.strToDate(fromDate);
			final Date td = DateFormatUtils.strToDate(toDate);
			
			stsb.setFromDate(fd);
			stsb.setFromDateStr(fromDate);
			stsb.setToDate(td);
			stsb.setToDateStr(toDate);

			final ProvinceBean province = this.masterService.getProvinceByProvinceId(provinceId);
			HospitalBean hospital = null;
			if(hospitalCode != null && hospitalCode.length() > 0){
				hospital = this.masterService.getHospitalByHCode(hospitalCode);
			}
			final SummaryTrackPageRepNo pageRepno = stsb.getPageRepNo();
			pageRepno.setHospital(hospital);
			pageRepno.setProvince(province);
			pageRepno.setSearch(true);
			pageRepno.setRepNo(repNo);
			pageRepno.setAuthTypeSelect(authType);
			stsb.setPageRepNo(pageRepno);

			List<String> lstRepNos = null;
			//step 1 get data transfer
			final List<SummaryTrackChartBean> beanTransfers = this.summaryTrackService.getRepNoByHospitalCodeTransfer(hospitalCode, fd, td, repNo, authType);
			lstRepNos = this.addListRepNo(beanTransfers, lstRepNos);
			//step 2 get data payable
			final List<SummaryTrackChartBean> beanPayables = this.summaryTrackService.getRepNoByHospitalCodePayable(hospitalCode, fd, td, repNo, authType);
			lstRepNos = this.addListRepNo(beanPayables, lstRepNos);
			//step 3 get data budget
			final List<SummaryTrackChartBean> beanBudgets = this.summaryTrackService.getRepNoByHospitalCodeProcessBudget(hospitalCode, fd, td, repNo, authType);
			lstRepNos = this.addListRepNo(beanBudgets, lstRepNos);
			//step 4 get data petition
			final List<SummaryTrackChartBean> beansPetitions = this.summaryTrackService.getRepNoByHospitalCodeProcessPetition(hospitalCode, fd, td, repNo, authType);
			lstRepNos = this.addListRepNo(beansPetitions, lstRepNos);
			//step 5 get data compensation
			final List<SummaryTrackChartBean> beanCheckCompensations = this.summaryTrackService.getRepNoByHospitalCodeProcessCheckCompensation(hospitalCode, fd, td, repNo, authType);
			if(beansPetitions != null || beanCheckCompensations != null || beanBudgets != null || beanPayables != null || beanTransfers != null){
				results = super.responseSuccess();
				maps.put(ESummaryStatus.COMPENSATION.getStatus(), beanCheckCompensations == null ? new ArrayList<SummaryTrackChartBean>() : beanCheckCompensations);
				maps.put(ESummaryStatus.PATITION.getStatus(), beansPetitions == null ? new ArrayList<SummaryTrackChartBean>() : beansPetitions);
				maps.put(ESummaryStatus.BUDGET.getStatus(), beanBudgets == null ? new ArrayList<SummaryTrackChartBean>() : beanBudgets);
				maps.put(ESummaryStatus.PAYABLE.getStatus(), beanPayables == null ? new ArrayList<SummaryTrackChartBean>() : beanPayables);
				maps.put(ESummaryStatus.TRANSFER.getStatus(), beanTransfers == null ? new ArrayList<SummaryTrackChartBean>() : beanTransfers);
				results.setDatas(maps);
			}else{
				results = super.responseSuccessNullData();
			}
			
			request.getSession().setAttribute(SessionConstr.SESSION_SUMMARY, stsb);
		}catch(Exception e){
			this.log.error(e.getMessage());
			results = super.responseError(MsgConstr.ERROR_EXCEPTION_CODE, e.getMessage());
			e.printStackTrace();
		}
		
		return results;
	}
	
	private List<String> addListRepNo(final List<SummaryTrackChartBean> lstChartBeans, List<String> lstRepNos){
		lstRepNos = lstRepNos == null ? new ArrayList<String>() : lstRepNos;
		
		if(lstChartBeans != null && lstChartBeans.size() > 0){
			for(final SummaryTrackChartBean bean : lstChartBeans){
				lstRepNos.add(bean.getRepNo());
			}
		}
		
		return lstRepNos;
	}
}
